﻿using System;
using System.Drawing;
using Space.Library;
using Space.Library.Extensions;
using Space.Model.Entity;

namespace Space.Model.Fsm
{
    /// <summary>
    /// Sets orientation to most accurate direction.
    /// Moves the ship in a straight line to the target. 
    /// </summary>
    public class FiringState : IState<Ship>
    {
        private const int FiringTime = 1000; // next weapon
        private readonly Ship _target;
        private Vector2D _targetPoint;
        private long _firingTimeLeft;
        private EffectBase _effect;
        public bool AcceptInput { get { return false; } }

        public Ship Owner { get; set; }

        public FiringState(Ship target)
        {
            _target = target;
        }

        public void Enter()
        {
            _targetPoint = _target.Pos;
            SetActiveWeapon();
        }

        private void SetActiveWeapon()
        {
            _firingTimeLeft = FiringTime; // 1 second
            _effect = new EffectBase(Owner, _target);
            Owner.Effects.Add(_effect);
        }

        public void Execute(long timestep)
        {
            if (_firingTimeLeft < 0) 
            {
                // TODO: Do damage.
                _target.ShipModel.ShieldTop -= 20; 

                // TODO: Next weapon.
                Owner.Effects.Remove(_effect);
                Owner.Fsm.ChangeState(new NothingState<Ship>());
                return;
            }

            _firingTimeLeft -= timestep;
        }

        public void Exit()
        {
            // Clear steering
        }
    }
}
